import java.util.*;

public class RandomStrategy implements Strategy{
	private LinkedList<String> Directions;
	private int width;
	private int height;
	/**
	 * returns direction of monsters movement
	 */
	private void Init(Thing map[][]) {
		if(Directions == null) Directions = new LinkedList<String>();
		if(!Directions.contains("Up")) Directions.add("Up");
		if(!Directions.contains("Right")) Directions.add("Right");
		if(!Directions.contains("Down")) Directions.add("Down");
		if(!Directions.contains("Left")) Directions.add("Left");
		width = map.length;
		height = map[0].length;
	}
	public String SelectDirection(Thing map[][], int xMonster, int yMonster, int xPacman, int yPacman) {
		Init(map);
		String dir = Directions.get(new Random().nextInt(4));
		if ((dir == "Up") && ((yMonster == 0) || ((map[xMonster][yMonster-1] != null) && (map[xMonster][yMonster-1].getClass() == Wall.class)))) {
			return SelectDirection(map, xMonster, yMonster, xPacman, yPacman);	
		}	
		if ((dir == "Right") && ((xMonster == width-1) || ((map[xMonster+1][yMonster] != null) && (map[xMonster+1][yMonster].getClass() == Wall.class)))) {
			return SelectDirection(map,  xMonster, yMonster, xPacman, yPacman);
		}
		if ((dir == "Down") && ((yMonster == height-1) || ((map[xMonster][yMonster+1] != null) && (map[xMonster][yMonster+1].getClass() == Wall.class)))) {
			return SelectDirection(map, xMonster, yMonster, xPacman, yPacman);
		}
		if ((dir == "Left") && ((xMonster ==0) || ((map[xMonster-1][yMonster] != null) && (map[xMonster-1][yMonster].getClass() == Wall.class)))) {
			return SelectDirection(map,  xMonster, yMonster, xPacman, yPacman);
		}
		
			
		return dir;
	}
	
}

